// pages/home/home.js
let app = getApp();
let testAddr = app.globalData.testAddr;
let imgAddr = app.globalData.imgurl;
let bmap = require('../../utils/bmap-wx.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inquirys:[],
    sortArr: [{
      image: '../../image/home/home_icon_1.png',
      title: '水泵'
    }, {
      image: '../../image/home/home_icon_2.png',
      title: '供水设备'
    }, {
      image: '../../image/home/home_icon_3.png',
      title: '消防器材'
    }, {
      image: '../../image/home/home_icon_4.png',
      title: '阀门'
    }, {
      image: '../../image/home/home_icon_5.png',
      title: '控制柜'
    }, {
      image: '../../image/home/home_icon_6.png',
      title: '法兰管件'
    }, {
      image: '../../image/home/home_icon_7.png',
      title: '管道'
    }, {
      image: '../../image/home/home_icon_8.png',
      title: '电动机'
    }, {
      image: '../../image/home/home_icon_9.png',
      title: '发动机'
    }, {
      image: '../../image/home/home_icon_10.png',
      title: '全部分类'
    }],
    newestOrderInfo: "",
    newestOrderKeywords: "",
    articleTitle: "",
    articleContent: "",
    articleTime: "",
    articleImgUrl: "",
    inquiryTitle: "",
    inquiryContent: "",
    district: "",
    scrollTop: 0,
    popstatus:false
  },
  // 展示弹窗
  showpop:function(){
    var sta = this.data.popstatus
    this.setData({
      popstatus: !sta
    })
  },
  // 一键询价
  go_inquiry:function(){
    wx.navigateTo({
      url: '../cardbark/inquirysheet/inquirysheet'
    })
  },
  //获取菜单列表
  queryMenuList: function() {
    let that = this;
    app.util.request({
      url: testAddr + "menuItem/menu",
      method: "POST",
      data: {

      },
      success: function(res) {
        let menus = res.data;
        for (let i in menus) {
          menus[i].iconSrc = imgAddr + "menu/" + menus[i].iconSrc;
        };
        that.setData({
          sortArr: menus
        });

      },
      fail: function(res) {

      },
      complete: function(res) {

      },
    });
  },
  //获取最新交易
  queryNewestOrder: function(res) {
    let that = this;
    app.util.request({
      url: testAddr + "facility_order/getNewsRecord",
      method: "POST",
      data: {

      },
      success: function(res) {
        let data = res.data;
        that.setData({
          newestOrderInfo: data.mycompanyName + data.myguestname + "与" + data.paycompanyName + data.payguestname + "达成一笔交易",
          newestOrderKeywords: data.menuName
        });
      },
      fail: function(res) {

      },
      complete: function(res) {

      },
    });
  },
  jumpTrans: function() {
    wx.navigateTo({
      url: '../detail/transaction/transaction',
    })
  },
  //最新商业圈
  queryNewestArticle: function() {
    let that = this;
    app.util.request({
      url: testAddr + "article/getNewArticle",
      method: "POST",
      data: {

      },
      success: function(res) {
        let data = res.data[0];
        that.setData({
          articleTitle: data.postname,
          articleContent: data.content,
          articleTime: data.createtime,
          articleImgUrl: imgAddr +"article/"+data.imgUrl,
        });
      },
      fail: function(res) {

      },
      complete: function(res) {

      },
    });
  },
  //跳转商业圈页面
  jumpBusiness: function() {
    wx.navigateTo({
      url: '../home/business/business',
    })
  },
  //附近的商家
  jumpNear: function() {
    wx.navigateTo({
      url: '../detail/nearby/nearby',
    })
  },
  //机电市场
  jumpMarket: function() {
    wx.navigateTo({
      url: '../detail/market/market',
    })
  },
  //招商加盟
  jumpJoin: function() {
    wx.navigateTo({
      url: '../detail/Join/Join',
    })
  },
  //最新询价信息
  queryNewestInquiry() {
    let that = this;
    app.util.request({
      url: testAddr + "enquiry/getNewEnquiry",
      method: "POST",
      data: {

      },
      success: function(res) {
        let inquirys = JSON.parse(res.data.result);
        that.setData({
          inquirys: inquirys,
        });
      },
      fail: function(res) {

      },
      complete: function(res) {

      },
    });
  },
  jumpProduct: function(event) {
    let typeId = event.currentTarget.dataset.id;
    let typeName = event.currentTarget.dataset.name;
    wx.navigateTo({
      url: '../cardbark/shoplist/shoplist?id=' + typeId + "&name=" + typeName,
    })
  },
  //跳转发布页
  jumpRelease(){
    wx.navigateTo({
      url: '../release/release',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.queryMenuList();
    this.queryNewestOrder();
    this.queryNewestArticle();
    this.queryNewestInquiry();
    let that = this;
    var BMap = new bmap.BMapWX({
      ak: "eR60uAw1XXZb0k3cDcU8i7q2bHXsPWGG"
    });
    var fail = function(data) {

    };
    var success = function(data) {
      that.setData({
        district: data.originalData.result.addressComponent.district
      });
    }
    // 发起regeocoding检索请求 
    BMap.regeocoding({
      fail: fail,
      success: success
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let that = this;
    // 滚动条的高度增加
    let interval = setInterval(() => {
      if (that.data.scrollTop < 2500) {
        that.setData({
          scrollTop: that.data.scrollTop + 10
        });
      } else {
        that.setData({
          scrollTop: 0
        });
      }
    }, 500)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})